//
//  DataSource.h
//  YuchBox
//
//  Created by Ivan on 11-10-7.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>

#import "IvanNetStream.h"
#import "IvanSqliteDB.h"
#import "Message.h"
#import "Configuration.h"
#import "Email.h"
#import "Attachment.h"
#import "Account.h"


#define CLIENT_VERSION          14

#define CLIENT_LANGUAGE_ZH      0
#define CLIENT_LANGUAGE_ZH_CN   0
#define CLIENT_LANGUAGE_ZH_HK   1
#define CLIENT_LANGUAGE_OTHER   2

#define INIT_EMAILS_ARRAY_SIZE      30
#define INIT_ATTACHMENT_MAP_SIZE    30
#define INIT_HASHCODE_MAP_SIZE      100

@class Email;

typedef enum {
    // springboard event
    DSEventSBLoginSuccessful    = 1,
    DSEventSBLoginFailed,
    DSEventSBRecvMessage,
    DSEventSBReady,
    
    
    //
    DSEventOpEmailRead,
    DSEventOpCheckEmailState,
    
    // the base net connect error
    DSEventErrorFromNet,
    DSEventNetErrorOperationCannotBeCompleted,
    DSEventNetErrorUnknown,
} DataSourceEvent;

@protocol DataSourceDelegate
- (void)callBackHandle:(DataSourceEvent)eventCode info:(id)info;

@end


#define DATA_SOURCE [DataSource sharedDataSource]
@interface DataSource : NSObject <IvanNetStreamDelegate>
{
    // configuration information
    Configuration           *_configuration;              // the configuration information, include loginInfo
    
    // mail 
    NSMutableArray          *_emails;
    NSMutableArray          *_emailsForSend;              // email buffer waiting for send
    NSMutableDictionary     *_recvMailHashCodeMap;
    
    NSMutableDictionary     *_attachments;                // for all attachments
    
    NSInteger               _currentMaxEmailUid;          // for databases
    
    NSObject<DataSourceDelegate> *_currentDelegate;
    // weibo
    NSMutableArray  *_weibos;
}
@property (retain, nonatomic) Configuration *configuration;
@property (retain, nonatomic) NSMutableArray *emails;

+ (DataSource *)sharedDataSource;
+ (void)checkRunTimeEnvironment;

- (id)initSingleton;

- (Boolean)connect;
- (Boolean)login;
- (void)connectAndLogin;
- (void)logout;

#pragma mark -
#pragma mark IvanNetStreamDelegate
- (void)getData:(NSData *)receivedData flag:(int)flag;
- (void)readInStream:(NSInputStream *)inStream;
- (NSData *)preProcessData:(UInt8 *)rawData length:(NSInteger)length outLength:(NSInteger *)outLength;
- (void)callBackHandle:(IvanNetStreamEvent)eventCode info:(id)info;

@end

@interface DataSource (private) 
- (void)initConfiguration;

- (NSInteger)processMessage:(NSInputStream *)inStream;
- (void)processRecvMail:(NSInputStream *)inStream;
- (void)processSentMail:(NSInputStream *)inStream;
- (void)processMailAttach:(NSInputStream *)inStream;

- (void)sendLogin:(Configuration *)configuration;
- (void)sendMail:(Email *)email;
- (void)sendConfirmMsgWithHashCode:(NSInteger)hashcode;
- (void)sendFetchAttachment:(Attachment *)attachment;
- (void)sendEmailAttachmentIntro:(Email *)email;
- (void)sendAttachment:(Attachment *)attachment;

- (void)sendKeepAlive;

@end